<template>
  <a-date-picker v-model:value="year" picker="year" value-format="YYYY"/>
  <a-select v-model:value="ter" style="width: 120px">
    <a-select-option value="01">学期一</a-select-option>
    <a-select-option value="02">学期二</a-select-option>
  </a-select>
  <a-button type="link" :loading="loading" @click="getGrades">搜索</a-button>
  <a-table :columns="columns" :data-source="dataSource" bordered>
    <template #bodyCell="{ column, text, record }">
      <template v-if="['name', 'age', 'address'].includes(column.dataIndex)">
        <div>
          <a-input
              v-if="editableData[record.key]"
              v-model:value="editableData[record.key][column.dataIndex]"
              style="margin: -5px 0"
          />
          <template v-else>
            {{ text }}
          </template>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'operation'">
        <div class="editable-row-operations">
          <span v-if="editableData[record.key]">
            <a-typography-link @click="save(record.key)">Save</a-typography-link>
            <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
              <a>Cancel</a>
            </a-popconfirm>
          </span>
          <span v-else>
            <a @click="edit(record.key)">Edit</a>
          </span>
        </div>
      </template>
    </template>
  </a-table>
</template>
<script setup lang="ts">
import { cloneDeep } from 'lodash-es';
import {defineComponent, onMounted, reactive, ref} from 'vue';
import type { UnwrapRef } from 'vue';
import myAxios from "../../plugins/myAxios";
import {message} from "ant-design-vue";
import type { Dayjs } from 'dayjs';
import dayjs from "dayjs";
const columns = [

  {
    title: '课程名',
    dataIndex: 'courseName',
    width: '15%',
  },
  {
    title: '学号',
    dataIndex: 'studentNo',
    width: '10%',
  },
  {
    title: '分数',
    dataIndex: 'score',
    width: '10%',
  },
  {
    title: '学期',
    dataIndex: 'term',
    width: '10%',
  },



];
interface DataItem {
  key: string;
  name: string;
  age: number;
  address: string;
}
const data: DataItem[] = [];
for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

const scoresDate = ref()

const dataSource = ref(scoresDate);
const editableData: UnwrapRef<Record<string, DataItem>> = reactive({});

const edit = (key: string) => {
  editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
};
const save = (key: string) => {
  Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
  delete editableData[key];
};
const cancel = (key: string) => {
  delete editableData[key];
};


onMounted(()=>{
  // getCourseList()
})
const getCourseList = async (year:any,ter:any) =>{
  const res = await myAxios.post("students/getScoresInfo",{
    year,
    ter
  })
  if (res.data){
    scoresDate.value = res.data
    // courseDate.value = courseDate.value.map(item=>{
    //   item.createTime = dayjs(item.createTime).format('YYYY-MM-DD')
    //   return item
    // })
    // console.log(res.data)
  }else {
    message.error(res.message)
  }
}


// const params = ref({
//   year:'',
//   days:'',
// })
const year = ref()
const ter = ref('')
const loading = ref(false)

const getGrades = async () =>{
  loading.value = true

  // year.value = (dayjs(year.value).format("YYYY"))
  // console.log(year.value)
  ter.value = (ter.value)
  getCourseList(year.value,ter.value)
  loading.value = false
  // console.log(params)
}
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
